package com.lc.railway.ui.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hjq.bar.OnTitleBarListener;
import com.hjq.bar.TitleBar;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.lc.railway.R;
import com.lc.railway.http.request.EmailJuzhangAddApi;
import com.lc.railway.http.request.EmailZhengweiAddApi;
import com.lc.railway.http.request.MailboxJuzhangApi;
import com.lc.railway.http.request.MailboxZhengweiApi;
import com.lc.railway.http.response.MailBoxJuzhangBean;
import com.lc.railway.http.response.RegisterBean;
import com.lc.railway.manager.InputTextManager;
import com.lc.railway.ui.dialog.FieldsDialog;
import com.lc.railway.util.CourseUtils;
import com.lc.widget.layout.SettingBar;
import com.lc.widget.view.ClearEditText;

import org.jetbrains.annotations.NotNull;

import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MailboxActivity extends WaterActivity {
    @BindView(R.id.title_bar_top)
    TitleBar titleBarTop;
    @BindView(R.id.et_mail_name)
    EditText etMailName;
    @BindView(R.id.et_mail_work)
    ClearEditText etMailWork;
    @BindView(R.id.et_mail_message)
    EditText etMailMessage;
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.tv_chongzhi)
    TextView tvChongzhi;
    @BindView(R.id.sb_honor)
    SettingBar sbHonor;
    @BindView(R.id.tv_tixian)
    TextView tvTixian;
    @BindView(R.id.sb_message)
    SettingBar sbMessage;
    private BaseQuickAdapter<MailBoxJuzhangBean.DataBean, BaseViewHolder> adapter;
    private String type;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_mailbox;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
        type = getIntent().getStringExtra("type");

        InputTextManager.with(this)
                .addView(etMailName)
                .addView(etMailWork)
                .addView(etMailMessage)
                .setMain(tvChongzhi)
                .build();
        titleBarTop.setOnTitleBarListener(new OnTitleBarListener() {
            @Override
            public void onLeftClick(View v) {
                finish();
            }

            @Override
            public void onTitleClick(View v) {

            }

            @Override
            public void onRightClick(View v) {
                FieldsDialog dialog=new FieldsDialog(getContext(),MailboxActivity.this,titleBarTop.getTitle().toString());
                dialog.show();
            }
        });
        adapter = new BaseQuickAdapter<MailBoxJuzhangBean.DataBean, BaseViewHolder>(R.layout.item_message) {
            @Override
            protected void convert(@NotNull BaseViewHolder holder, MailBoxJuzhangBean.DataBean dataBean) {
                holder.setText(R.id.tv_name, dataBean.getName() + "    手机：" + dataBean.getMobile());
                holder.setText(R.id.content, dataBean.getContent());
                holder.setText(R.id.liuyan_date, "留言时间：" + dataBean.getCreate_time());
                holder.setText(R.id.tv_reply_date, "回复时间：" + CourseUtils.timeStamp2Date(dataBean.getReply_time(),"yyyy-MM-dd HH:mm:ss"));
                holder.setText(R.id.tv_reply, dataBean.getReply());
                if (dataBean.getStatus().equals("0")) {
                    holder.setGone(R.id.ll_reply, true);
                } else {
                    holder.setVisible(R.id.ll_reply, true);
                }

            }
        };
        recyclerView.setAdapter(adapter);
        if (type.equals("局长")) {
            titleBarTop.setTitle("局长信箱");
            sbHonor.setLeftText("局长信箱");
            api_news_lists();
        }else {
            titleBarTop.setTitle("政委信箱");
            sbHonor.setLeftText("政委信箱");
            api_news_lists2();
        }
    }

    @OnClick({R.id.tv_chongzhi, R.id.tv_tixian})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_chongzhi:
                if (type.equals("局长"))
                    api_email_juzhang_add();
                else
                    api_email_zhengwei_add();

                break;
            case R.id.tv_tixian:
                etMailMessage.setText("");
                etMailName.setText("");
                etMailWork.setText("");
                break;
        }
    }

    private void api_email_juzhang_add() {
        EasyHttp.post(this)
                .api(new EmailJuzhangAddApi().setName(etMailName.getText().toString().trim()).setMobile(etMailWork.getText().toString().trim()).setContent(etMailMessage.getText().toString().trim()))
                .request(new HttpCallback<RegisterBean>(this) {
                    @Override
                    public void onSucceed(RegisterBean data) {
//
                        toast(data.getError_msg());
                        if (data.getError_code() == 200) {
                            finish();
                        }
                    }
                });

    }

    private void api_email_zhengwei_add() {
        EasyHttp.post(this)
                .api(new EmailZhengweiAddApi().setName(etMailName.getText().toString().trim()).setMobile(etMailWork.getText().toString().trim()).setContent(etMailMessage.getText().toString().trim()))
                .request(new HttpCallback<RegisterBean>(this) {
                    @Override
                    public void onSucceed(RegisterBean data) {
//
                        toast(data.getError_msg());
                        if (data.getError_code() == 200) {
                            finish();
                        }
                    }
                });

    }

    private void api_news_lists() {
        EasyHttp.post(this)
                .api(new MailboxJuzhangApi())
                .request(new HttpCallback<MailBoxJuzhangBean>(this) {
                    @Override
                    public void onSucceed(MailBoxJuzhangBean data) {
//
                        if (data.getError_code() == 200) {
                            adapter.setNewData(data.getData());

                        }
                    }
                });


    }

    private void api_news_lists2() {
        EasyHttp.post(this)
                .api(new MailboxZhengweiApi())
                .request(new HttpCallback<MailBoxJuzhangBean>(this) {
                    @Override
                    public void onSucceed(MailBoxJuzhangBean data) {
//
                        if (data.getError_code() == 200) {
                            adapter.setNewData(data.getData());

                        }
                    }
                });


    }
}
